package org.seven.cpcrkapp;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

import org.seven.cpcrkapp.activity.FindActivity;
import org.seven.cpcrkapp.activity.InActivity;
import org.seven.cpcrkapp.activity.OutActivity;
import org.seven.cpcrkapp.activity.SettingActivity;
import org.seven.cpcrkapp.activity.UserActivity;
import org.seven.cpcrkapp.constant.GlobalContants;
import org.seven.cpcrkapp.util.DialogUtils;
import org.seven.cpcrkapp.util.HttpUtils;
import org.seven.cpcrkapp.util.CacheUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.menu_in)
    LinearLayout menu_in;
    @BindView(R.id.menu_out)
    LinearLayout menu_out;
    @BindView(R.id.menu_refresh)
    LinearLayout menu_refresh;

    private static boolean isExit = false;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            super.handleMessage(message);
            isExit = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (!isExit) {
            isExit = true;
            DialogUtils.alert(R.string.app_exit);
            handler.sendEmptyMessageDelayed(0, 2000);
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.setting:
                Intent intent = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.info:
                DialogUtils.alert(R.string.app_copyright);
                break;
            default:
                break;
        }
        return true;
    }

    @OnClick(R.id.menu_in)
    void onClickMenuIn(View v) {
        Intent intent = new Intent(MainActivity.this, UserActivity.class);
        intent.putExtra(UserActivity.MENU, getString(R.string.menu_in));
        startActivity(intent);
    }


    @OnClick(R.id.menu_out)
    void onClickMenuOut(View v) {
        Intent intent = new Intent(MainActivity.this, UserActivity.class);
        intent.putExtra(UserActivity.MENU, getString(R.string.menu_out));
        startActivity(intent);
    }

    @OnClick(R.id.menu_find)
    void onClickMenuFind(View v) {
        Intent intent = new Intent(MainActivity.this, FindActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.menu_refresh)
    void onClickMenuRefresh(View v) {
        final ProgressDialog pd = DialogUtils.createProgressDialog(MainActivity.this);
        pd.show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String ph = HttpUtils.get("/app/core/dict/ph");
                    String lh = HttpUtils.get("/app/core/dict/lh");
                    String lc = HttpUtils.get("/app/core/dict/lc");
                    String ks = HttpUtils.get("/app/core/dict/ks");
                    CacheUtils.clear();
                    CacheUtils.put(CacheUtils.DICT_PH, ph);
                    CacheUtils.put(CacheUtils.DICT_LH, lh);
                    CacheUtils.put(CacheUtils.DICT_LC, lc);
                    CacheUtils.put(CacheUtils.DICT_KS, ks);
                    pd.dismiss();
                    runOnUiThread(new Runnable() {
                        public void run() {
                            DialogUtils.alert(R.string.app_success);
                        }
                    });
                } catch (Exception e) {
                    pd.dismiss();
                    final String msg = e.getMessage();
                    runOnUiThread(new Runnable() {
                        public void run() {
                            DialogUtils.alert(msg);
                        }
                    });
                }
            }
        }).start();
    }
}